﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CollegeShop.Api.ViewObject;
using CollegeShop.Api.BaseComponent;
using CollegeShop.Api.ViewObject.User;
using CollegeShop.Api.Infrastructure;
using CollegeShop.Api.Framework;
using System.Data;
using CollegeShop.Api.DBUtility;

namespace CollegeShop.Api.User
{
    public class AccountBiz : IComponent
    {
        private AccountBiz() { }
        public static AccountBiz Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (key)
                    {
                        if (instance == null)
                        {
                            instance = new AccountBiz();
                        }
                    }
                }
                return instance as AccountBiz;
            }
        }

        public UserVO GetUserWithPassword(int college, string userName, string password)
        {
            var user = WithCache(String.Format("AccountBiz.GetUserWithPassword_{0}", userName),
                () => { return new t_user().GetModel(college, userName); }, 600);
            if (user != null)
            {
                if (user.Password == HashAlgorithm.Encrypt(password, EncryptMode.sha1))
                {
                    return new UserVO()
                    {
                        userName = userName,
                        nickName = user.NickName
                    };
                }
            }
            return null;
        }
        public List<DeliveryVO> GetDeliveryInfos(int college, string userName)
        {
            return WithCache("AccountBiz.GetDeliveryInfos_" + userName,
                () =>
                {
                    if (new t_user().Exists(college, userName))
                    {
                        var ds = new t_deliveryaddress().GetList(college, "userName = '" + userName + "'");
                        if (ds != null && ds.Tables.Count > 0)
                        {
                            List<DeliveryVO> list = new List<DeliveryVO>();
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                DeliveryVO dev = new DeliveryVO();
                                dev.Address = row["Address"].ToString();
                                dev.DormitoryId = row["DormitoryId"].ToInt32();
                                dev.Id = row["ID"].ToInt32();
                                dev.LinkMan = row["LinkMan"].ToString();
                                dev.Phone = row["LinkPhone"].ToString();
                                dev.UserName = userName;
                                list.Add(dev);
                            }
                            return list;
                        }
                    }
                    return null;
                }, 180);
        }

        public List<DeliveryVO> GetDeliveryInfos(int college, string anonymousCode, int ClientType)
        {
            return WithCache(String.Format("AccountBiz.GetDeliveryInfos_{0}_{1}", anonymousCode, ClientType),
                () =>
                {
                    StringBuilder sql = new StringBuilder();
                    sql.Append("select d.id,d.dormitoryId,d.address,d.linkman,d.linkphone,a.clientCode from t_deliveryaddress d join t_anonymous a on d.anonymousId = a.id where a.clientCode = '");
                    sql.Append(anonymousCode);
                    sql.Append("' and a.clienttype=");
                    sql.Append(ClientType);
                    sql.Append(";");
                    using (var reader = DbHelperMySQL.ExecuteReader(college, sql.ToString()))
                    {
                        List<DeliveryVO> list = new List<DeliveryVO>();
                        while (reader.Read())
                        {
                            DeliveryVO item = new DeliveryVO();
                            item.Address = reader["Address"].ToString();
                            item.AnonymousCode = anonymousCode;
                            item.DormitoryId = reader["DormitoryID"].ToInt32();
                            item.Id = reader["Id"].ToInt32();
                            item.LinkMan = reader["LinkMan"].ToString();
                            item.Phone = reader["LinkPhone"].ToString();
                            item.UserName = "";
                            list.Add(item);
                        }
                        return list;
                    }
                }, 180);
        }
    }
}
